//
//  DDShareView.m
//  DingDongZP
//
//  Created by Jyh on 16/3/24.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDShareView.h"
#import "DDShareManager.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SDWebImageManager.h"

@interface DDShareView()

//@property (nonatomic, strong) NSString *shareTitle;
//@property (nonatomic, strong) NSString *shareDesc;
//@property (nonatomic, strong) NSString *shareH5;
//@property (nonatomic, strong) NSString *shareLogoUrl;
@property (nonatomic, strong) UIImage *shareImg;
@property (nonatomic, strong) UIViewController *target;


@property (nonatomic, assign) DDShareType shareType;


@end

@implementation DDShareView


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupData];
    }
    
    return  self;
}

- (void)awakeFromNib {
    [self setupData];
}

//显示
-(void) show {
    
    UIViewController *rootvc = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    self.target = rootvc;
    
    if (self.superview == nil) {
        
        [self.target.view addSubview:self];
    }
    
    
    
    CGRect originalBound = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 0);
    originalBound.origin.y +=210;
    
    self.actionView.frame = originalBound;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect newBounds = CGRectMake(0, SCREEN_HEIGHT - 210, SCREEN_WIDTH, 0);
        
        self.actionView.frame = newBounds;
    }];
    
}
//
-(void)dissMiss {
    
    [self hide];
}

- (void) setupData {
    
    _shareImg = [[UIImage alloc] init];
    
    WEAKSELF
    self.userInteractionEnabled = true;
    UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        
        [weakSelf dissMiss];
        
    }];
    [self addGestureRecognizer:tap];
    
    [self.cancelButton bk_whenTapped:^{
        
        [weakSelf dissMiss];
    }];
    
}

- (IBAction)onShareButtonClick:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    
    //tag值在xib中定义
    if ([self.delegate respondsToSelector:@selector(shareViewButtonClick:)]) {
        
        [self.delegate shareViewButtonClick:button.tag];
    }
    
    
    switch (button.tag) {
            
        case DDShareTypeWXTimeLine:
            
            self.shareType = DDShareTypeWXTimeLine;
            [self shareToWeiXinWithType:DDShareTypeWXTimeLine];
            break;
            
        case DDShareTypeWeiXinSession:
            
            self.shareType = DDShareTypeWeiXinSession;
            [self shareToWeiXinWithType:DDShareTypeWeiXinSession];
            break;
            
        case DDShareTypeWeiBo:
            
            self.shareType = DDShareTypeWeiBo;
            [self shareToWeibo];
            break;
            
        case DDShareTypeSMS:
            
            self.shareType = DDShareTypeSMS;
            [self showMessageView];
            break;
            
        default:
            
            break;
    }
    
    
}

- (void)setShareModel:(DDShareModel *)shareModel {
    
    _shareModel = shareModel;
    
    
    if (![_shareModel.shareLogoUrl hasPrefix:@"https"]) {
        
        NSData *data = [[NSData alloc] initWithContentsOfFile:_shareModel.shareLogoUrl];
        self.shareImg = [UIImage imageWithData:data];
        return;
    }
    
    SDImageCache *cache = [[LDCacheManager shareInstance].sdImageManger imageCache];
    UIImage *image = [cache imageFromDiskCacheForKey:_shareModel.shareLogoUrl];
    
    if (image == nil) {
        
        return;
    }
    
    UIImage *clipimage = [DDUtils handleImage:image withSize:CGSizeMake(120, 120)];
    self.shareImg = clipimage;
    
}

- (void)layoutSubviews
{
    self.frame = [[UIScreen mainScreen] bounds];
    [super layoutSubviews];
}


- (void)hide
{
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect originalBound = self.actionView.frame;
        originalBound.origin.y = kScreenHeight;
        self.actionView.frame = originalBound;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

- (void)shareToWeiXinWithType:(DDShareType)shareType {
    
    int scence = 0;
    if (shareType == DDShareTypeWeiXinSession) {
        
        scence = WXSceneSession;
    } else {
        
        scence = WXSceneTimeline;
    }
    
    [[DDShareManager shareInstance] shareToWeiXinWithScene:scence title:_shareModel.shareTitle_WeiXin description:_shareModel.shareDesc  webpageUrl:_shareModel.shareH5 image:_shareImg isVideo:false];
}

- (void)shareToWeibo
{
    
    [[DDShareManager shareInstance] shareToWeibo:nil title:_shareModel.shareTitle_WeiBo description: _shareModel.shareDesc webpageUrl:_shareModel.shareH5 image:_shareImg VC:self isVideo:NO];
}


- (void)showMessageView
{
    if( [MFMessageComposeViewController canSendText] ){
        
        MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc] init];
        controller.body = [NSString stringWithFormat:@"%@ %@ \n%@",_shareModel.shareTitle_SMS,_shareModel.shareDesc,_shareModel.shareH5];
        controller.messageComposeDelegate = self;
        
        [self.target presentViewController:controller animated:YES completion:nil];
        
        [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"Invite"];//修改短信界面标题
    }else{
        
        [UIAlertView bk_alertViewWithTitle:@"提醒" message:@"设备不支持短信功能"];
    }
    
}

/**
 *  发送完短信的回调
 *
 *  @param controller
 *  @param result
 */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    
    [controller dismissViewControllerAnimated:NO completion:^{
        
    }];//关键的一句   不能为YES
    
    switch (result) {
            
        case MessageComposeResultCancelled:
            
            [[LDShowHUB shareInstance] showToastNoti:@"发送取消"];
            break;
        case MessageComposeResultFailed:// send failed
            
            [[LDShowHUB shareInstance] showToastNoti:@"发送失败"];
            break;
        case MessageComposeResultSent:
            
            [[LDShowHUB shareInstance] showToastNoti:@"发送成功"];
            break;
        default:
            break;
    }
}


@end
